import os
import time
from loguru import logger


# 这里是为了永远将日志文件夹放在当前工程目录下，而不至于当项目下有多个子目录时
def projectpath():
    pwd = os.getcwd()
    while len(pwd.split('\\')) > 4:
        pwd = os.path.dirname(pwd)  # 向上退一级目录
    pwd = 'D:/data/log'
    # print(pwd)
    return pwd


def __logfun(isfile=True):
    out_p = projectpath()
    t = str(time.strftime("%Y%m%d", time.localtime()))
    logfile = "%s/%s.log" % (out_p, t)
    logger.add(logfile, rotation='1 day')
    return logger


log = __logfun()
if __name__ == '__main__':
    log.debug('Thi%s is a debug message.')
    log.info('This is an info message.')
    log.warning('This is a warning message.')
    log.error('This is an error message.')
    log.critical('This is a critical message.')
